package cn.bocweb.spinning.view.common;

import android.app.IntentService;
import android.content.Intent;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import cn.bocweb.spinning.model.Bean.SecondDownloadBean;
import cn.bocweb.spinning.model.Imp.BaseModelImp;
import cn.bocweb.spinning.util.AES256Utils;
import cn.bocweb.spinning.util.DeviceUtil;
import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by chenchangxing on 16/7/27.
 */
public class SecondTestService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    String timeline;

    public SecondTestService() {
        super("SecondTestService");
        DeviceUtil.Log("服务开启");

        timeline = String.valueOf(System.currentTimeMillis());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Call call = new BaseModelImp().mServer.secondDownload(timeline, DeviceUtil.getSign(timeline));
        call.enqueue(new Callback<SecondDownloadBean>() {

            @Override
            public void onResponse(Response<SecondDownloadBean> response, Retrofit retrofit) {
                if (response.body().getReturn_code() == 0) {
                    SecondDownloadBean bean = new SecondDownloadBean();
                    bean.setTimelimit(AES256Utils.getInstance().addSecureToStr(response.body().getTimelimit()));
                    bean.setMsg(AES256Utils.getInstance().addSecureToStr(response.body().getMsg()));
                    bean.setRequireScore(AES256Utils.getInstance().addSecureToStr(response.body().getRequireScore()));
                    bean.setTotalSocre(AES256Utils.getInstance().addSecureToStr(response.body().getTotalSocre()));
                    List<SecondDownloadBean.DataBean> dataBeanArrayList = new ArrayList<>();
                    for (int i = 0; i < response.body().getData().size(); i++) {
                        SecondDownloadBean.DataBean dataBean = new SecondDownloadBean.DataBean();
                        dataBean.setTitle(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getTitle()));
                        dataBean.setId(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getId()));
                        dataBean.setTtype(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getTtype()));
                        dataBean.setExplain(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getExplain()));
                        dataBean.setLibtype(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getLibtype()));
                        if (response.body().getData().get(i).getOption() != null) {
                            List<String> list = new ArrayList<>();
                            for (int j = 0; j < response.body().getData().get(i).getOption().size(); j++) {
                                list.add(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getOption().get(j)));
                            }
                            dataBean.setOption(list);
                        }
                        dataBean.setRight(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getRight()));
                        dataBean.setTid(AES256Utils.getInstance().addSecureToStr(response.body().getData().get(i).getTid()));
                        dataBeanArrayList.add(dataBean);
                    }
                    bean.setData(dataBeanArrayList);


                    DeviceUtil.Log("题目获取成功");
                    File file = new File(getCacheDir().getAbsolutePath() + "/secondTest.txt");
                    ObjectOutputStream stream;
                    try {
                        stream = new ObjectOutputStream(
                                new FileOutputStream(file));
                        stream.writeObject(bean);
                    } catch (IOException e) {
                        DeviceUtil.Log("封装题目错误 = " + e.toString());
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
    }

//    public void onEvent(SecondDownloadBean bean) {
//        if (bean.getReturn_code() == 0) {
//            DeviceUtil.Log("题目获取成功");
//            File file = new File(getCacheDir().getAbsolutePath() + "/secondTest");
//            if (!file.exists()) {
//                DeviceUtil.Log("题目文件不存在");
//                file.mkdir();
//            }
//            ObjectOutputStream stream;
//            try {
//                stream = new ObjectOutputStream(
//                            new FileOutputStream(
//                                getCacheDir().getAbsolutePath() + "/secondTest"));
//                stream.writeObject(stream);
//            } catch (IOException e) {
//                DeviceUtil.Log("封装题目错误 = " + e.toString());
//            }
//        }
//    }

    @Override
    public void onDestroy() {
        DeviceUtil.Log("服务终止");
        super.onDestroy();
    }
}
